package com.oracle.lingshangejia.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.oracle.lingshangejia.R;

public class HealthCheckExtendableListAdapter extends BaseExpandableListAdapter {
    private String[] groupStr;

    public HealthCheckExtendableListAdapter(String[] groupStr) {
        this.groupStr = groupStr;
    }

    @Override
    public int getGroupCount() {
        return this.groupStr.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupStr[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    static class GroupViewHolder {
        TextView tvTitle;
    }

    /**
     * 获取显示指定组的视图对象
     *
     * @param groupPosition 组位置
     * @param isExpanded    该组是展开状态还是伸缩状态
     * @param convertView   重用已有的视图对象
     * @param parent        返回的视图对象始终依附于的视图组
     */

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            groupViewHolder = new GroupViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.health_group_list, parent, false);
            groupViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.label_group_normal);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.tvTitle.setText(groupStr[groupPosition]);
        return convertView;
    }

    /**
     * 获取一个视图对象，显示指定组中的指定子元素数据。
     *
     * @param groupPosition 组位置
     * @param childPosition 子元素位置
     * @param isLastChild   子元素是否处于组中的最后一个
     * @param convertView   重用已有的视图(View)对象
     * @param parent        返回的视图(View)对象始终依附于的视图组
     * @return
     * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, View,
     * ViewGroup)
     */ // R.layout.health_child_list_3,
    private int[] childItems = {R.layout.health_child_list_0, R.layout.health_child_list_1, R.layout.health_child_list_2,
           R.layout.health_child_list_4, R.layout.health_child_list_5, R.layout.health_child_list_6
            , R.layout.health_child_list_7, R.layout.health_child_list_8, R.layout.health_child_list_9, R.layout.health_child_list_10
            , R.layout.health_child_list_11, R.layout.health_child_list_12, R.layout.health_child_list_13, R.layout.health_child_list_14
            , R.layout.myhealth_item_0, R.layout.myhealth_item_1, R.layout.myhealth_item_2, R.layout.myhealth_item_3, R.layout.myhealth_item_4
            , R.layout.myhealth_item_5, R.layout.myhealth_item_6, R.layout.myhealth_item_7, R.layout.myhealth_item_8
            , R.layout.myhealth_item_9, R.layout.myhealth_item_10, R.layout.myhealth_item_11
    };
    private View[] convertViews = new View[childItems.length];

    public View[] getConvertViews() {
        return convertViews;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertViews[groupPosition] == null) {
            if (groupPosition < childItems.length) {
                convertViews[groupPosition] = LayoutInflater.from(parent.getContext()).inflate(childItems[groupPosition], parent, false);
            }
        }
        return convertViews[groupPosition];
    }

    //指定位置上的子元素是否可选中
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
